﻿using System;
using System.Collections.Generic;
using GeoAPI.Geometries;
using NetTopologySuite.Geometries;

namespace NetTopologySuite.EdgeGraph
{
	public class EdgeGraphBuilder
	{
		public static EdgeGraph Build(IEnumerable<IGeometry> geoms)
		{
			EdgeGraphBuilder edgeGraphBuilder = new EdgeGraphBuilder();
			edgeGraphBuilder.Add(geoms);
			return edgeGraphBuilder.GetGraph();
		}

		public EdgeGraph GetGraph()
		{
			return this.graph;
		}

		public void Add(IGeometry geometry)
		{
			geometry.Apply(new GeometryComponentFilter(delegate(IGeometry c)
			{
				if (c is ILineString)
				{
					this.Add(c as ILineString);
				}
			}));
		}

		public void Add(IEnumerable<IGeometry> geometries)
		{
			foreach (IGeometry geometry in geometries)
			{
				this.Add(geometry);
			}
		}

		private void Add(ILineString lineString)
		{
			ICoordinateSequence coordinateSequence = lineString.CoordinateSequence;
			for (int i = 1; i < coordinateSequence.Count; i++)
			{
				Coordinate coordinate = coordinateSequence.GetCoordinate(i - 1);
				Coordinate coordinate2 = coordinateSequence.GetCoordinate(i);
				this.graph.AddEdge(coordinate, coordinate2);
			}
		}

		private readonly EdgeGraph graph = new EdgeGraph();
	}
}
